package com.example.androidmainshixun.view;

import android.app.Activity;



import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.androidmainshixun.R;

import java.io.IOException;

public class feedbackActivity extends AppCompatActivity{

    private static final int PICK_IMAGE_REQUEST = 1;
    private EditText feedbackEditText;
    private ImageView imagePreview;
    private Uri imageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feedback);

        feedbackEditText = findViewById(R.id.feedbackEditText);
        Button addImageButton = findViewById(R.id.addImageButton);
        Button submitButton = findViewById(R.id.submitButton);
        imagePreview = findViewById(R.id.imagePreview);

        addImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openFileChooser();
            }
        });

        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitFeedback();
            }
        });
    }

    private void openFileChooser() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, PICK_IMAGE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            imageUri = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
                imagePreview.setImageBitmap(bitmap);
                imagePreview.setVisibility(View.VISIBLE);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void submitFeedback() {
        String feedback = feedbackEditText.getText().toString().trim();
        if (feedback.isEmpty()) {
            Toast.makeText(this, "请输入反馈信息", Toast.LENGTH_SHORT).show();
            return;
        }

        // Here you can handle the feedback submission, e.g., send it to a server or save locally.
        // For this example, we just display a Toast message.
        Toast.makeText(this, "反馈已提交", Toast.LENGTH_SHORT).show();

        // Clear the feedback input and image preview
        feedbackEditText.setText("");
        imagePreview.setImageDrawable(null);
        imagePreview.setVisibility(View.GONE);
    }
}